'use strict';

const Service = require('../core/base_service');

class LeaveService extends Service {
  // 提交请假
  async addLeave (payload) {
    let res
    try {
      res = await this.ctx.model.Leave.create(payload)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }

  // 审批
  async updateLeave (payload) {
    let res
    try {
      res = await this.ctx.model.Leave.findByIdAndUpdate(payload._id, payload)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }

  // 获取信息
  async getLeave (params) {
    let res
    try {
      res = await this.ctx.model.Leave.find(params)
      // console.log('getLeave', res)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }

  // 获取信息
  async getLeavemohu (params) {
    let res
    try {
      res = await this.ctx.model.Leave.find({ name: { $regex: params.name } })
      // console.log('getLeave', res)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }

  // 删除
  async deleteLeave (payload) {
    let res
    try {
      res = await this.ctx.model.Leave.findByIdAndRemove(payload._id)
      return this.success(res)
    } catch (error) {
      console.log(error)
    }
  }
}

module.exports = LeaveService;
